$(document).ready(function() {
	getMe();
	$("#personal").click(function(event) {
		getMe();
	});
	$("#addparent").click(function(event) {
		show("addparentmsg");
		$("#submit0").click(function(){
			addparent();
		});
	});
	$("#update").click(function(event) {
		getMeup();
	});
	//返回首页
	$("#imgBt").click(function(event) {
		window.location.href= "./home.html";
	});
});
function show(idStr){
	$("#personmsg").removeClass("show").addClass("hide");
	$("#addparentmsg").removeClass("show").addClass("hide");
	$("#updatemsg").removeClass("show").addClass("hide");
	$("#"+idStr).removeClass("hide").addClass("show");
}

//查看信息
function getMe(){
	show("personmsg");
	$.ajax({
		url: '../user/getMe',
		type: 'GET',
		dataType: 'json',
		data: {level: '0'},
		success: function(data){
			console.log("ajax success!!!");
			console.log(data);
			var user = data.user;
			$("#username").html(user.username);
			$("#email").html(user.email);
			$("#class").html(user.classnum);
			getParent();
		},
		error: function(){
			console.log("ajax fail!!!");
			alert(errorThrown);
		}
	});
}
function getParent(){
	$.ajax({
		url: '../parent/getParent',
		type: 'GET',
		dataType: 'json',
		data: {level: '0'},
		success: function(data){
			console.log("ajax success!!!");
			console.log(data);
			var parent = data.parent;
			if(parent == null){
				$("#parent").html("未添加");
			}else{
				$("#parent").html(parent.username);
			}
			
		},
		error: function(){
			console.log("ajax fail!!!");
			alert(errorThrown);
		}
	});
}
//修改信息
function getMeup(){
	show("updatemsg");
	$.ajax({
		url: '../user/getMe',
		type: 'GET',
		dataType: 'json',
		data: {level: '0'},
		success: function(data){
			console.log("ajax success!!!");
			console.log(data);
			var user = data.user;
			$("#showname").html(user.username);
			$("#showemail").html(user.email);
			$("#class1").html(user.classnum);
			$("#submit1").click(function(){
				//处理更新
				var pass = $("#password1").val();
				var pass1 = $("#password11").val();
				if(pass != pass1){
					alert("两次密码不相同");
					return;
				}
				updateUser(pass);
			});
		},
		error: function(){
			console.log("ajax fail!!!");
			alert(errorThrown);
		}
	});
}
function updateUser(pass) {
	var userInfo = new Object();
	userInfo.password = pass;
	$.ajax({
		url : '../user/modify',
		type : 'POST',
		dataType : 'json',
		contentType:"application/json",
		data:JSON.stringify(userInfo),
		success : function(data) {
			if(data.states == "OK"){
				window.location.href= "./personcenter.html";
			}else{
				alert(data.states);
				return;
			}
		},
		error : function() {
			console.log("ajax fail!!!");
			alert(errorThrown);
		}
	});
}
//添加家长
function addparent(){
 	var name =  $("#name0").val();
 	var email =  $("#email0").val();
 	var password =  $("#password0").val();
 	var password1 =  $("#password00").val();
 	if(password != password1){
 		alert("两次密码不相同");
		return;
 	}
 	var userInfo = new Object();
 	userInfo.userid = name;
 	userInfo.username = name;
 	userInfo.password = password;
 	userInfo.email = email;
 	userInfo.level = "2";
 	$.ajax({
		url : '../user/addParent',
		type : 'POST',
		dataType : 'json',
		contentType:"application/json",
		data:JSON.stringify(userInfo),
		success : function(data) {
			if(data.states == "OK"){
				window.location.href= "./personcenter.html";
			}else{
				alert(data.states);
				return;
			}
		},
		error : function() {
			console.log("ajax fail!!!");
			alert(errorThrown);
		}
	});
}